"""
在数据库中创建表  DDL
"""
from pymysql import Connection
conn = None
try:
    # 1. 创建数据库连接对象
    conn = Connection(
        host='localhost',
        port=3306,
        user='root',
        password='root',
        charset='utf8',
        database='db2'
    )

    # 2. 获取游标对象
    cursor = conn.cursor()
    # 3. 定义SQL语句
    sql = """
        create table t_user(
            id int primary key auto_increment,
            u_name varchar(20) not null,
            gender varchar(20),
            age int,
            address varchar(20)
        )
    """

    # 4. 使用游标对象操作SQL语言
    cursor.execute(sql)
    print('表创建成功')


except Exception as e:
    print(e)
finally:
    if conn:
        conn.close()